
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class FlutterWidgetLifecycle extends StatefulWidget {
  @override
  _FlutterWidgetLifecycleState createState() => _FlutterWidgetLifecycleState();
}
///生命周期方法
///1.创建时期 ：createState , initState
///2.更新时期 ：didChangeDependencies , build, didUpdateWidget
///1.销毁时期 ：deactivate , dispose
class _FlutterWidgetLifecycleState extends State<FlutterWidgetLifecycle> {
  static const platform = const MethodChannel('samples.flutter.io/battery');
  @override
  void initState() {
    // TODO: implement initState
    print('---initState-----');
    super.initState();
  }

  Future<String> _startActivity() async {
    await platform.invokeMethod('startActivity');
    return "success";
  }
  ///在所依赖的state对象发生改变时调用
  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    print('---didChangeDependencies---');
    super.didChangeDependencies();
  }
  ///在didChangeDependencies后会被立即调用
  @override
  Widget build(BuildContext context) {
    print('---build---');
    return Scaffold(
      appBar: AppBar(
        title: Text('生命周期'),
      ),
      body: Center(
        child: FlatButton(
          child: Text("跳转activity"),
          onPressed: () {
            _startActivity();
            //_pickImage();
          },
        ),
      ),
    );
  }

  @override
  void didUpdateWidget(FlutterWidgetLifecycle oldWidget) {
    print('---didUpdateWidget---');
    super.didUpdateWidget(oldWidget);
  }

  @override
  void deactivate() {
    // TODO: implement deactivate
    print('---deactivate---');
    super.deactivate();
  }
  ///被销毁时调用
  @override
  void dispose() {
    // TODO: implement dispose
    print('---dispose---');
    super.dispose();
  }


  _pickImage() {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext bc) {
          return Stack(
            children: <Widget>[
              Container(
                height: 30.0,
                width: double.infinity,
                color: Colors.black54,
              ),
              Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(25),
                      topRight: Radius.circular(25),
                    )),
              ),
              Container(
                child: FlatButton(
                  child: Container(
                    alignment: Alignment.center,
                    padding:
                    EdgeInsets.only(top: 33.0, bottom: 33.0),
                    child: Text(
                      "bottomSheet的内容",
                    ),
                  ),
                ),
              ),
            ],
          );
        });
//    showModalBottomSheet(
//        context: context,
//        builder: (context) {
//          return Container(
//            height: 160,
//            child: ListView.separated(
//                itemBuilder: (context, index) {
//                  ListTile listTile;
//                  if (index == 0) {
//                    listTile = ListTile(
//                      title: Text("拍照"),
//                    );
//                  } else {
//                    listTile = ListTile(
//                      title: Text("从相册选择"),
//                    );
//                  }
//                  return listTile;
//                },
//                separatorBuilder: (context, index) => Divider(
//                      height: 1,
//                    ),
//                itemCount: 2),
//          );
//        });
  }
}
